public class SumNumbers {

    StringBuffer stringBuffer = new StringBuffer();
    int ans = 0;
    public int sumNumbers(TreeNode root) {
        process(root);
        return ans;
    }

    public void process(TreeNode node) {
        stringBuffer.append(node.val);
        if (node.left == null && node.right == null) {
            ans += Integer.parseInt(stringBuffer.toString());
        } else {
            if (node.left != null) {
                process(node.left);
            }
            if (node.right != null) {
                process(node.right);
            }
        }
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
    }
}
